package org.jim.server.processor.chat;

import cn.hutool.core.util.StrUtil;
import org.jim.core.ImChannelContext;
import org.jim.core.ImConst;
import org.jim.core.config.ImConfig;
import org.jim.core.helper.cs.ICsChatHelper;
import org.jim.core.helper.cs.ICsSessionHelper;
import org.jim.core.packets.Message;
import org.jim.core.packets.pub.consts.ChatType;
import org.jim.core.packets.pub.consts.MsgType;
import org.jim.core.packets.pub.pojo.JimChatBody;
import org.jim.core.utils.KeyUtil;
import org.jim.server.config.ImServerConfig;
import org.jim.server.processor.SingleProtocolCmdProcessor;
import org.jim.server.util.ChatKit;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @author WChao
 * @date 2018年4月3日 下午1:13:32
 */
@Component
public class CsAsyncChatMessageProcessor implements SingleProtocolCmdProcessor {
    @Autowired
    private ICsChatHelper csChatHelper;
    @Autowired
    private ICsSessionHelper csSessionHelper;

    @Override
    public void process(ImChannelContext imChannelContext, Message message) {
        JimChatBody chatBody = (JimChatBody) message;
        ImServerConfig imServerConfig = ImConfig.Global.get();
        //开启持久化
        boolean isStore = ImServerConfig.ON.equals(imServerConfig.getIsStore());
        if (isStore) {
            //存储到最近100条客户端唯一id 使用list的rpush+ltrim listname 1 99 保留100个元素,此处可以排除重复,list 只存储客户端发的消息id
            //RedisCacheManager.getCache(ImConst.USER_KEY.USER_SEND_CACHE_NAME).listPushTail(chatBody.getFromUserId(), chatBody.getCreateTime(), USER_KEY.USER_RECENT_LIMIT);
            //1.持久化存储到数据库
            csChatHelper.writeDbMessage(chatBody);
            String uid = imChannelContext.getUserId();
            boolean isTempSession = chatBody.getSessionId().indexOf(TEMP) != -1;
            boolean isOnline = false;
            if (!isTempSession) {
                isOnline = ChatKit.isOnline(chatBody.getTo(), isStore);
            }
            //2.存储离线消息
            if (!isOnline||isTempSession) {
                //用户发消息
                if (ImConst.USER_OPERATE.equals(chatBody.getOperateType())) {
                    //客服的离线消息
                    csChatHelper.writeOfflineMessage(PUSH, KeyUtil.formatKey(MSG_KEY.CS_CS_OFFLINE,"csTenantId",chatBody.getCsTenantId(),"userId",chatBody.getFrom()), chatBody);
                }
                //客服发消息
                else {
                    //用户的离线消息
                    csChatHelper.writeOfflineMessage(PUSH, KeyUtil.formatKey(MSG_KEY.CS_USER_OFFLINE,"csTenantId",chatBody.getCsTenantId(),"userId",chatBody.getTo()), chatBody);
                }
            }

            //3.刷新客服会话session,im的聊天会话不做刷新操作,仅仅用来记录存在不存在的
            if (ChatType.CHAT_TYPE_CS.getNumber() == chatBody.getChatType()) {
                //如：0:text、1:image、2:voice、3:vedio、4:music、5:news
                String lastMsg = "";
                if (chatBody.getMsgType().equals(MsgType.MSG_TYPE_TEXT.getNumber())) {
                    lastMsg = StrUtil.subSufByLength(chatBody.getContent(), 19) + "...";
                } else if (chatBody.getMsgType().equals(MsgType.MSG_TYPE_IMG.getNumber())) {
                    lastMsg = "[图片]";
                }
                csSessionHelper.refresh(chatBody.getSessionId(), chatBody.getCreateTime(), lastMsg, ImConst.CS_OPERATE);
            }
        }
    }
}
